package cn.keniykey.mc.landterritory.config;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;

import cn.keniykey.mc.landterritory.LandTerritoryMain;
import cn.keniykey.mc.landterritory.land.LandEnterLeaveCheck.EEnterLeaveTipLocation;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class ConfigurationManager {
	private static final String FILE_NAME = "config.yml";
	private static final int VERSION = 1;
	
	private ConfigurationManager() {}
	
	private static YamlConfiguration getConfigurationYamlForDir(boolean force) {
		final LandTerritoryMain main = LandTerritoryMain.getPlugin();
		final File dirfile = main.getDataFolder();
		dirfile.mkdirs();
		final File langfile = new File(dirfile, FILE_NAME);
		if(force || !langfile.exists()) {
			if(main.getResource(FILE_NAME) != null) {
				main.saveResource(FILE_NAME, true);
			}else {
				main.getLogger().warning(FILE_NAME + " null");
				return new YamlConfiguration();
			}
		}
		return YamlConfiguration.loadConfiguration(langfile);
	}
	
	public static String AdminPer;
	public static int LandInOut_Tick;
	
	public static String Territory_Create_Per,Territory_Create_VipPer;
	public static int Territory_Create_Price,Territory_Create_Level;
	public static double Territory_Create_Cost,Territory_Create_CostVip;
	public static List<String> Territory_Create_Flags;
	public static String Territory_Limit_Per;
	public static List<Long> Territory_Limit_Size;
	public static List<Integer> Territory_Limit_XYZ;
	public static Set<String> Territory_Limit_Worlds;
	public static String Territory_Sell_SellPer,Territory_Sell_BuyPer,Territory_Sell_NoTaxPer;
	public static double Territory_Sell_Tax;
	
	public static String Community_Create_Per,Community_Create_VipPer;
	public static int Community_Create_Price,Community_Create_Level;
	public static double Community_Create_Cost,Community_Create_CostVip;
	public static List<String> Community_Create_Flags;
	public static String Community_Limit_Per;
	public static List<Long> Community_Limit_Size;
	public static List<Integer> Community_Limit_XYZ;
	public static Set<String> Community_Limit_Worlds;
	public static String Community_Sell_SellPer,Community_Sell_BuyPer,Community_Sell_NoTaxPer;
	public static double Community_Sell_Tax;
	
	public static String Ground_Create_Per;
	public static int Ground_Create_Price,Ground_Create_Level;
	public static List<String> Ground_Create_Flags;
	public static String Ground_Sell_SellPer,Ground_Sell_BuyPer,Ground_Sell_NoTaxPer;
	public static double Ground_Sell_Tax;
	
	public static int View_ClickSpeed;
	
	public static int View_LandMain_LandLimit_NewPlayer,View_LandMain_LandLimit_Price;
	public static String View_LandMain_LandLimit_Per;
	
	public static String View_LandInfo_Reduce_Per,View_LandInfo_Expand_Per;
	public static int View_LandInfo_Reduce_Price,View_LandInfo_Expand_Price,View_LandInfo_Change_Level;
	public static String View_LandInfo_Change_Per,View_LandInfo_Owner,View_LandInfo_Pos;
	public static String View_LandInfo_Transfer_Per,View_LandInfo_Transfer_VipPer;
	public static int View_LandInfo_Transfer_Countdown,View_LandInfo_Transfer_Wait,View_LandInfo_Transfer_NoSafe;
	public static Material View_LandInfo_Show_Block;
	public static String View_LandInfo_Show_Per;
	public static int View_LandInfo_Show_AmountLimit,View_LandInfo_Show_Second,View_LandInfo_Show_SecondAmount;
	public static int View_LandInfo_Show_Countdown;
	public static String View_LandInfo_Tip_Per;
	public static int View_LandInfo_Tip_Length;
	public static EEnterLeaveTipLocation View_LandInfo_Tip_Loc;
	public static String View_LandInfo_System,View_LandInfo_Delete;
	public static int View_LandInfo_Name_Length;
	public static String View_LandInfo_Name_Per,View_LandInfo_Level,View_LandInfo_Overlap;
	
	public static String View_LandCreate_Migrate_Per,View_LandCreate_Tool_Per;
	public static int View_LandCreate_Migrate_Price,View_LandCreate_Tool_Price;
	public static Material View_LandCreate_Tool_Item;
	
	public static String View_LandFlags_Add,View_LandFlags_Delete,View_LandFlags_Rest;
	
	public static int View_FlagInfo_ContentLimit,View_FlagInfo_ParameterLimit;
	public static String View_FlagInfo_Add,View_FlagInfo_AddAll,View_FlagInfo_Remove,View_FlagInfo_Clear;
	
	public static int View_Choose_Countdown;
	
	public static void reloadConfiguration() {
		YamlConfiguration yaml = getConfigurationYamlForDir(false);
		if(yaml.getInt("version") != VERSION) {
			yaml = getConfigurationYamlForDir(true);
			LandTerritoryMain.getPlugin().getLogger().warning("配置文件 " + FILE_NAME + " 的version 不是 " + VERSION);
		}
		AdminPer = yaml.getString("adminper","land.admin");
		LandInOut_Tick = Math.max(yaml.getInt("landinout.tick", 14), 1);
		reloadTerritorySetting(yaml);
		reloadCommunitySetting(yaml);
		reloadGroundSetting(yaml);
		reloadViewSetting(yaml);
	}
	private static void reloadTerritorySetting(final YamlConfiguration yaml) {
		Territory_Create_Per = yaml.getString("territory.create.per");
		Territory_Create_Price = Math.max(yaml.getInt("territory.create.price"), 0);
		Territory_Create_Cost = Math.max(yaml.getDouble("territory.create.cost"), 0d);
		Territory_Create_CostVip = Math.min(yaml.getDouble("territory.create.costvip"), Territory_Create_Cost);
		Territory_Create_VipPer = yaml.getString("territory.create.vipper");
		Territory_Create_Level = Math.max(yaml.getInt("territory.create.level"), 1);
		Territory_Create_Flags = Collections.unmodifiableList(yaml.getStringList("territory.create.flags"));
		Territory_Limit_Per = yaml.getString("territory.limit.per");
		Territory_Limit_Size = Collections.unmodifiableList(yaml.getLongList("territory.limit.size"));
		Territory_Limit_XYZ = Collections.unmodifiableList(yaml.getIntegerList("territory.limit.xyz"));
		Territory_Limit_Worlds = Collections.unmodifiableSet(new HashSet<>(yaml.getStringList("territory.limit.worlds")));
		Territory_Sell_SellPer = yaml.getString("territory.sell.sellper");
		Territory_Sell_BuyPer = yaml.getString("territory.sell.buyper");
		Territory_Sell_NoTaxPer = yaml.getString("territory.sell.notaxper");
		Territory_Sell_Tax = Math.max(yaml.getDouble("territory.sell.tax"), 0d);
	}
	private static void reloadCommunitySetting(final YamlConfiguration yaml) {
		Community_Create_Per = yaml.getString("community.create.per");
		Community_Create_Price = Math.max(yaml.getInt("community.create.price"), 0);
		Community_Create_Cost = Math.max(yaml.getDouble("community.create.cost"), 0d);
		Community_Create_CostVip = Math.min(yaml.getDouble("community.create.costvip"), Community_Create_Cost);
		Community_Create_VipPer = yaml.getString("community.create.vipper");
		Community_Create_Level = Math.max(yaml.getInt("community.create.level"), (Territory_Create_Level+2));
		Community_Create_Flags = Collections.unmodifiableList(yaml.getStringList("community.create.flags"));
		Community_Limit_Per = yaml.getString("community.limit.per");
		Community_Limit_Size = Collections.unmodifiableList(yaml.getLongList("community.limit.size"));
		Community_Limit_XYZ = Collections.unmodifiableList(yaml.getIntegerList("community.limit.xyz"));
		Community_Limit_Worlds = Collections.unmodifiableSet(new HashSet<>(yaml.getStringList("community.limit.worlds")));
		Community_Sell_SellPer = yaml.getString("community.sell.sellper");
		Community_Sell_BuyPer = yaml.getString("community.sell.buyper");
		Community_Sell_NoTaxPer = yaml.getString("community.sell.notaxper");
		Community_Sell_Tax = Math.max(yaml.getDouble("community.sell.tax"), 0d);
	}
	private static void reloadGroundSetting(final YamlConfiguration yaml) {
		Ground_Create_Per = yaml.getString("ground.create.per");
		Ground_Create_Price = Math.max(yaml.getInt("ground.create.price"), 0);
//		Ground_Create_Cost = Math.max(yaml.getDouble("ground.create.cost"), 0d);
//		Ground_Create_CostVip = Math.max(yaml.getDouble("ground.create.costvip"), Ground_Create_Cost);
//		Ground_Create_VipPer = yaml.getString("ground.create.vipper");
		Ground_Create_Level = Math.max(yaml.getInt("ground.create.level"), (Community_Create_Level+1));
		Ground_Create_Flags = Collections.unmodifiableList(yaml.getStringList("ground.create.flags"));
//		Ground_Limit_Per = yaml.getString("ground.limit.per");
//		Ground_Limit_Size = Collections.unmodifiableList(yaml.getLongList("ground.limit.size"));
//		Ground_Limit_XYZ = Collections.unmodifiableList(yaml.getIntegerList("ground.limit.xyz"));
//		Ground_Limit_Worlds = Collections.unmodifiableList(yaml.getStringList("ground.limit.worlds"));
		Ground_Sell_SellPer = yaml.getString("ground.sell.sellper");
		Ground_Sell_BuyPer = yaml.getString("ground.sell.buyper");
		Ground_Sell_NoTaxPer = yaml.getString("ground.sell.notaxper");
		Ground_Sell_Tax = Math.max(yaml.getDouble("ground.sell.tax"), 0d);
	}
	private static void reloadViewSetting(final YamlConfiguration yaml) {
		View_ClickSpeed = Math.max(yaml.getInt("view.clickspeed"), 0);
		
		View_LandMain_LandLimit_NewPlayer = yaml.getInt("view.landmain.landlimit.newplayer");
		View_LandMain_LandLimit_Price = yaml.getInt("view.landmain.landlimit.price");
		View_LandMain_LandLimit_Per = yaml.getString("view.landmain.landlimit.per");
		
		View_LandInfo_Reduce_Per = yaml.getString("view.landinfo.reduce.per");
		View_LandInfo_Reduce_Price = Math.max(yaml.getInt("view.landinfo.reduce.price"), 0);
		View_LandInfo_Expand_Per = yaml.getString("view.landinfo.expand.per");
		View_LandInfo_Expand_Price = Math.max(yaml.getInt("view.landinfo.expand.price"), 0);
		View_LandInfo_Change_Per = yaml.getString("view.landinfo.change.per");
		View_LandInfo_Change_Level = Math.max(yaml.getInt("view.landinfo.change.level"), (Territory_Create_Level+1));
		View_LandInfo_Owner = yaml.getString("view.landinfo.owner");
		View_LandInfo_Pos = yaml.getString("view.landinfo.pos");
		View_LandInfo_Transfer_Per = yaml.getString("view.landinfo.transfer.per");
		View_LandInfo_Transfer_Countdown = Math.max(yaml.getInt("view.landinfo.transfer.countdown"), 0);
		View_LandInfo_Transfer_Wait = Math.max(yaml.getInt("view.landinfo.transfer.wait"), 0);
		View_LandInfo_Transfer_NoSafe = Math.max(yaml.getInt("view.landinfo.transfer.nosafe"), 5);
		View_LandInfo_Transfer_VipPer = yaml.getString("view.landinfo.transfer.vipper");
		View_LandInfo_Show_Per = yaml.getString("view.landinfo.show.per");
		View_LandInfo_Show_AmountLimit = yaml.getInt("view.landinfo.show.amountlimit");
		View_LandInfo_Show_Second = yaml.getInt("view.landinfo.show.second", 5);
		View_LandInfo_Show_SecondAmount = yaml.getInt("view.landinfo.show.secondamount", 40);
		View_LandInfo_Show_Block = Util.getMaterial(yaml.getString("view.landinfo.show.block"));
		View_LandInfo_Show_Countdown = Math.max(yaml.getInt("view.landinfo.show.countdown"), 0);
		View_LandInfo_Tip_Per = yaml.getString("view.landinfo.tip.per");
		View_LandInfo_Tip_Length =  Math.min(yaml.getInt("view.landinfo.tip.length"), 200);
		View_LandInfo_Tip_Loc = EEnterLeaveTipLocation.valueOf(yaml.getString("view.landinfo.tip.loc","chat").toUpperCase());
		View_LandInfo_System = yaml.getString("view.landinfo.system");
		View_LandInfo_Delete = yaml.getString("view.landinfo.delete");
		View_LandInfo_Name_Per = yaml.getString("view.landinfo.name.per");
		View_LandInfo_Name_Length = Math.max(Math.min(yaml.getInt("view.landinfo.name.length"), 30), 5);
		View_LandInfo_Level = yaml.getString("view.landinfo.level");
		View_LandInfo_Overlap = yaml.getString("view.landinfo.overlap");
		
		View_LandCreate_Migrate_Price = Math.max(yaml.getInt("view.landcreate.migrate.price"), 0);
		View_LandCreate_Migrate_Per = yaml.getString("view.landcreate.migrate.per");
		View_LandCreate_Tool_Price = Math.max(yaml.getInt("view.landcreate.tool.price"), 0);
		View_LandCreate_Tool_Per = yaml.getString("view.landcreate.tool.per");
		View_LandCreate_Tool_Item = Util.getMaterial(yaml.getString("view.landcreate.tool.item"));
		
		View_LandFlags_Add = yaml.getString("view.landflags.add");
		View_LandFlags_Delete = yaml.getString("view.landflags.delete");
		View_LandFlags_Rest = yaml.getString("view.landflags.rest");
		
		View_FlagInfo_ContentLimit = yaml.getInt("view.flaginfo.contentlimit");
		View_FlagInfo_ParameterLimit = yaml.getInt("view.flaginfo.parameterlimit");
		View_FlagInfo_Add = yaml.getString("view.flaginfo.add");
		View_FlagInfo_AddAll = yaml.getString("view.flaginfo.addall");
		View_FlagInfo_Remove = yaml.getString("view.flaginfo.remove");
		View_FlagInfo_Clear = yaml.getString("view.flaginfo.clear");
		
		View_Choose_Countdown = Math.max(yaml.getInt("view.choose.countdown"), 5);
	}
}